package li.kaeppe.travel.tracker.view;

import java.util.HashMap;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import li.kaeppe.travel.tracker.domain.Trip;
import li.kaeppe.travel.tracker.service.TripService;
import li.kaeppe.travel.tracker.web.service.WebAppConfigurationService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sun.jersey.api.view.Viewable;
import com.sun.jersey.spi.inject.Inject;

@Path("/")
public class Controller {

	/* **************************************************** */
	/*                      CONSTANTS                       */
	/* **************************************************** */
	
	private static final Log log = LogFactory.getLog(Controller.class);
	
	/* **************************************************** */
	/*                     MEMBER FIELDS                    */
	/* **************************************************** */
	
	@Inject("ttService")
	private TripService tripService;
	
	@Inject("webAppConfiguration")
	private WebAppConfigurationService webAppConfiguration;

	/* **************************************************** */
	/*                      CONSTRUCTOR                     */
	/* **************************************************** */
	
	/* **************************************************** */
	/*                    PUBLIC METHODS                    */
	/* **************************************************** */
	
	@GET @Produces("text/html")
    @Path("/view/{user}/{trip}")
    public Viewable getTrip(@PathParam("user") String user, @PathParam("trip") String tripKey) {
		return getTrip(user, tripKey, 5);
	}
	
	@GET @Produces("text/html")
    @Path("/view/{user}/{trip}/{level}")
    public Viewable getTrip(@PathParam("user") String user, 
    							@PathParam("trip") String tripKey, 
    							@PathParam("level") int level) {
		HashMap<String, String> values = new HashMap<String, String>();
		Trip trip = null;
		
		if(log.isDebugEnabled()) {
			log.debug("Loading trip for user = " + user + ", trip = " + tripKey);
		}
		
		trip = tripService.getTripByKey(tripKey, user);
		
		Viewable view;
		if(trip != null && trip.hasWayPoints()) {
			values.put("tripKey", tripKey);
			values.put("user", user);
			
			values.put("level", Integer.toString(level));
			values.put("title", trip.getTitle());
			values.put("description", trip.getDescription());
			values.put("googleMapApiVersion", webAppConfiguration.getGoogleMapApiVersion());
			values.put("googleMapApiKey", webAppConfiguration.getGoogleMapApiKey());
			values.put("centerLatitude", Double.toString( 
					(trip.getMaxLatitude().doubleValue() + trip.getMinLatitude().doubleValue()) * 0.5 ) );
			values.put("centerLongitude", Double.toString( 
					(trip.getMaxLongitude().doubleValue() + trip.getMinLongitude().doubleValue()) * 0.5 ) );
			values.put("minLatitude", trip.getMinLatitude().toString());
			values.put("maxLatitude", trip.getMaxLatitude().toString());
			values.put("minLongitude", trip.getMinLongitude().toString());
			values.put("maxLongitude", trip.getMaxLongitude().toString());
			view = new Viewable("index", values);
		} else {
			view = new Viewable("error", null);
		}
		
        return view;
	}

	/* **************************************************** */
	/*                  PROTECTED METHODS                   */
	/* **************************************************** */
	
	/* **************************************************** */
	/*                   PRIVATE METHODS                    */
	/* **************************************************** */
	
	
	/* **************************************************** */
	/*                  GETTERS AND SETTERS                 */
	/* **************************************************** */ 
	
	public TripService getTripService() {
		return tripService;
	}

	public void setTripService(TripService tripService) {
		this.tripService = tripService;
	}
	
	public WebAppConfigurationService getWebAppConfiguration() {
		return webAppConfiguration;
	}

	public void setWebAppConfiguration(
			WebAppConfigurationService webAppConfiguration) {
		this.webAppConfiguration = webAppConfiguration;
	}
	
}